﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace DBSourceToolsLib.CodeParser
{
    public class SourceFile
    {
        private string m_FileName;
        private string m_Directory;
        private string m_FullFileName;
        private string m_Text;

        public string Text
        {
            get { return m_Text; }
            //set { m_Text = value; }
        }

        ArrayList m_SourceLines = new ArrayList();

        public SourceLine ElementAt(int idx)
        {
            return (SourceLine)m_SourceLines[idx];
        }

        public int LineCount { get { return m_SourceLines.Count; } }

        public SourceFile()
        {

        }

        public void setText(string strText) {
            m_Text = strText;

            int iCurrIdx = 0;
            string strCurrLine = "";
            for (iCurrIdx = 0; iCurrIdx < strText.Length; iCurrIdx++)
            {
                char iCurrChar = strText[iCurrIdx];
                strCurrLine += strText[iCurrIdx];
                if (strCurrLine.Contains(Environment.NewLine))
                {
                    SourceLine oNewLine = new SourceLine(strCurrLine, m_SourceLines.Count);
                    m_SourceLines.Add(oNewLine);
                    strCurrLine = "";
                }
            }

            if (!String.IsNullOrEmpty(strCurrLine))
            {
                SourceLine oNewLine = new SourceLine(strCurrLine, m_SourceLines.Count);
                m_SourceLines.Add(oNewLine);
            }

        }

        public string DumpToString()
        {
            StringBuilder oStringBuilder = new StringBuilder();
            foreach( SourceLine oSourceLine in m_SourceLines)
                oStringBuilder.AppendLine( oSourceLine.dumpToString());
            return oStringBuilder.ToString();
        }

        public string GetErrorString()
        {
            StringBuilder oBuilder = new StringBuilder();
            foreach (SourceLine oSourceLine in m_SourceLines)
                if (oSourceLine.hasErrors())
                    oBuilder.Append(oSourceLine.ErrorsToString());
            return oBuilder.ToString();
        }


        internal void ClearErrors()
        {
            foreach (SourceLine oLine in m_SourceLines)
                oLine.ClearErrors();
        }
    }
}
